﻿using System;
using System.Collections.Generic;
using System.Data;

namespace FancyIdea.PingShui.Core.Model
{
    public class User : IComparable<User>
    {
        public string Code;
        public string Name;
        public string Permissions;

        public bool HasPermission(权限列表 perm)
        {
            if (string.IsNullOrEmpty(Permissions))
            {
                return false;
            }

            string p = 权限列表扩展.ToCodeString(perm);
            return p == Permissions || Permissions.StartsWith(p + ",")
                || Permissions.Contains("," + p);
        }

        public void Assign(User u)
        {
            if (u == null)
            {
                return;
            }

            Name = u.Name;
            Permissions = u.Permissions;
        }

        public int CompareTo(User other)
        {
            return Code.CompareTo(other.Code);
        }
    }

    public class UserList
    {
        private List<User> users = new List<User>();

        private int IndexOf(User user)
        {
            return user == null ? -1 : IndexOf(user.Code);
        }

        private int IndexOf(string code)
        {
            for (int i = 0; i < users.Count; i++)
            {
                if (users[i].Code == code)
                {
                    return i;
                }
            }
            return -1;
        }

        public void Add(User user)
        {
            if (user == null)
            {
                return;
            }

            int index = IndexOf(user);
            if (index >= 0)
            {
                users[index].Assign(user);
            }
            else
            {
                users.Add(user);
            }
        }

        public void Remove(User user)
        {
            if (user != null)
            {
                Remove(user.Code);
            }
        }

        public void Remove(string code)
        {
            int index = IndexOf(code);
            if (index >= 0)
            {
                users.RemoveAt(index);
            }
        }

        public User this[int i]
        {
            get { return users[i]; }
            set { users[i] = value; }
        }

        public User this[string code]
        {
            get
            {
                int index = IndexOf(code);
                return index < 0 ? null : users[index];
            }
        }

        public int Count { get { return users.Count; } }

        public DataTable ToDataTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("CODE"));
            table.Columns.Add(new DataColumn("NAME"));

            foreach (var user in users)
            {
                DataRow row = table.NewRow();
                row[0] = user.Code;
                row[1] = user.Name;
                table.Rows.Add(row);
            }
            return table;
        }

        public string[] GetCode()
        {
            List<string> list = new List<string>();
            foreach (var user in users)
            {
                list.Add(user.Code);
            }
            return list.ToArray();
        }
    }
}